package com.example.drcbackend.controller;

import ch.qos.logback.core.net.SyslogOutputStream;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.drcbackend.Mapper.UserMapper;
import com.example.drcbackend.entity.User;
import com.example.drcbackend.jwt.JwtUtils;
import com.example.drcbackend.jwt.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.checkerframework.checker.units.qual.A;
import java.util.ArrayList;
import java.util.List;

@RequestMapping("/user")
@RestController
@CrossOrigin
public class UserController {

    @Autowired
    private UserMapper um;


    public User getuser(String token){
        String uname = JwtUtils.getClaimsByToken(token).getSubject();
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getAccount,uname);
        User user = um.selectOne(lqw);
        return user;
    }

    public User getByUsername(String un,String psd){
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getAccount,un).eq(User::getPwd,psd);
        return um.selectOne(lqw);
    }

    @PostMapping("/register")
    public Result register(@RequestBody User user){
        if(getByUsername(user.getAccount(),user.getPwd())!=null)return Result.error().message("该账号已存在！");
        user.setRole(0);
        if(um.insert(user) > 0) return Result.ok().message("注册成功！");
        return Result.error().message("注册失败！");
    }

    @PostMapping("/login")
    public Result login(@RequestBody User u){
        User user=getByUsername(u.getAccount(),u.getPwd());
        if(user!=null){
            String tokem = JwtUtils.generateToken(user.getAccount());
            return Result.ok().data("token",tokem);
        }
        return Result.error().message("用户名或密码错误！");
    }

    @GetMapping("/info")
    public Result getInfo(String token){
        String uname = JwtUtils.getClaimsByToken(token).getSubject();
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getAccount,uname);
        User user = um.selectOne(lqw);
        String role="";
        switch (user.getRole()){
            case 0:role = "用户";break;
            case 1:role = "管理员";break;
        }
        ArrayList<String> rs = new ArrayList<>();
        rs.add(role);
        if(uname!=null)return Result.ok().data("name",user.getName()).data("roles",rs).data("id",user.getId());
        else return Result.error();
    }

    @PostMapping("/logout")
    public Result logout(){ return Result.ok(); }
    @PostMapping("/getuserdata")
    public Result getuserdata(String token){
        User user=getuser(token);
        return Result.ok().data("user",user);
    }
    @PostMapping("/edituser")
    public Result edituser(@RequestBody User user){
        //System.out.println(user);
        if(um.updateById(user) > 0 )return Result.ok().message("修改成功").data("user",user);
        return Result.error().message("修改失败");
    }
    @PostMapping("/updatepass")
    public Result updatepass(String password,String token){

        User user = getuser(token);
        System.out.println(password);
        user.setPwd(password);
        um.updateById(user);
        return Result.ok().message("修改成功！");
    }
    @PostMapping("/getallmanager")
    public Result getallmanager(){
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getRole,1);
        List<User> guser=um.selectList(lqw);
        return Result.ok().data("guser",guser);
    }
    @PostMapping("/getalluser")
    public Result getalluser(){
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getRole,0);
        List<User> yuser=um.selectList(lqw);
        return Result.ok().data("yuser",yuser);
    }
    @PostMapping("/editgsuser")
    public Result editgsuser(@RequestBody User user){
        um.updateById(user);
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getRole,1);
        List<User> guser=um.selectList(lqw);
        return Result.ok().data("guser",guser);
    }

    @PostMapping("/addgsuser")
    public Result addgsuser(@RequestBody User user){
        user.setRole(1);
        user.setAge(20);
        um.insert(user);

        return Result.ok();
    }



    @PostMapping("/editysuser")
    public Result editysuser(@RequestBody User user){
        um.updateById(user);
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getRole,0);
        List<User> yuser=um.selectList(lqw);
        return Result.ok().data("yuser",yuser);
    }


    @PostMapping("/addysuser")
    public Result addysuser(@RequestBody User user){
        user.setRole(0);
        user.setAge(20);
        um.insert(user);
        System.out.println(user);

        return Result.ok();
    }
//    @GetMapping("/all")
//    public Result all(int role){
//        List<User> users = getAll("all",role);
//        if(users!=null) return Result.ok().data("users",users);
//        else return Result.error();
//    }
//
//    @PostMapping("/edit")
//    public Result edit(@RequestBody User user){
//        if(um.updateById(user) > 0 )return Result.ok().data("users",getAll("all",user.getRole()));
//        return Result.error().message("修改失败");
//    }
//
//    @PostMapping("/delete")
//    public Result delete(int id,int role){
//        if(um.deleteById(id) > 0 )return Result.ok().data("users",getAll("all",role));
//        return Result.error().message("删除失败");
//    }
}
